package com.dsl.tms.model;

import com.dsl.tms.entity.redis.AdminRedis;
import com.dsl.tms.entity.result.Result;
import com.dsl.tms.enums.AdminLoginResult;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.DigestUtils;

import java.util.concurrent.TimeUnit;


public class LoginModel {

    public static String createToken(RedisTemplate redisTemplate,com.dsl.tms.entity.db.Admin admin){
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append(admin.getId());
        stringBuffer.append(admin.getUserName());
        stringBuffer.append(System.currentTimeMillis());
        String token = DigestUtils.md5DigestAsHex(stringBuffer.toString().getBytes());
        AdminRedis adminRedisRedis = new AdminRedis();
        adminRedisRedis.setUserId(admin.getId());
        adminRedisRedis.setUserName(admin.getUserName());
        adminRedisRedis.setAccess(admin.getRoles());
        adminRedisRedis.setAvator(admin.getAvator());
        redisTemplate.opsForValue().set(token, adminRedisRedis);
        redisTemplate.expire(token,3600,TimeUnit.SECONDS);
        return token;
    }

    public static Result getResult(AdminLoginResult adminLoginResult){
        Result result = new Result();
        result.setCode(adminLoginResult.getCode());
        result.setDescription(adminLoginResult.getDescription());
        return result;
    }


}
